package com.gnerv.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gnerv.author.dto.ArticalCountDTO;
import com.gnerv.author.entity.Commentary;
import com.gnerv.author.mapper.CommentaryMapper;
import com.gnerv.author.service.ICommentaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.author.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 博客平台-评论表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-19
 */
@Service
public class CommentaryServiceImpl extends ServiceImpl<CommentaryMapper, Commentary> implements ICommentaryService {

    @Autowired
    CommentaryMapper commentaryMapper;

    @Override
    public boolean createCommentary(Commentary commentary) {
        commentary.setbId(UUIDUtil.getLower());
        commentaryMapper.insert(commentary);
        return true;
    }

    @Override
    public boolean createOrUpdate(Commentary comment) {
        if (StringUtils.isEmpty(comment.getbId())) {
            comment.setbId(UUIDUtil.getLower());
            commentaryMapper.insert(comment);
        } else {
            Commentary commentary = new Commentary();
            commentary.setbId(comment.getbId());
            commentaryMapper.update(comment, new QueryWrapper<>(commentary));
        }
        return true;
    }

    @Override
    public boolean deleteCommentary(String bId) {
        Commentary commentary = new Commentary();
        commentary.setbId(bId);
        commentaryMapper.delete(new QueryWrapper<>(commentary));
        return true;
    }

    @Override
    public boolean updateCommentary(Commentary commentary) {
        commentaryMapper.updateById(commentary);
        return true;
    }

    @Override
    public Commentary selectCommentary(String bId) {
        Commentary commentary = new Commentary();
        commentary.setbId(bId);
        Commentary commentary1 = commentaryMapper.selectOne(new QueryWrapper<>(commentary));
        return commentary1;
    }

    @Override
    public List<Commentary> listCommentary(Commentary commentary) {
        List<Commentary> commentaryList = commentaryMapper.selectList(new QueryWrapper<>(commentary));
        return commentaryList;
    }


    @Override
    public List<ArticalCountDTO> countArticalByUnverify() {

        List<ArticalCountDTO> articalCountDTOS = commentaryMapper.selArticalComment();
        return articalCountDTOS;
    }

    @Override
    public List<Commentary> listUnverify(String aId) {
        Commentary commentary = new Commentary();
        commentary.setaId(aId);
        commentary.setState(0);
        List<Commentary> commentaries = commentaryMapper.selectList(new QueryWrapper<>(commentary));
        return commentaries;
    }
}
